package models.repository;

import models.entities.BrandEntity;
import models.entities.DuplicateEntity;
import models.entities.PictureEntity;
import models.entities.ProductEntity;
import models.helpers.DBSession;
import models.repository.common.ImageRepository;
import mybatis.mappers.ImageMapper;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.util.List;

@Deprecated
public class ImageRepositorySql implements ImageRepository {


    public PictureEntity getImageByMD5(String md5) throws IOException {
        SqlSession dbSession = DBSession.openSession();
        try {
            ImageMapper imageMapper = dbSession.getMapper(ImageMapper.class);
            return imageMapper.getImageByMD5(md5);
        } finally {
            if (dbSession != null) {
                dbSession.close();
            }
        }
    }

    public List<PictureEntity> getImagesWithoutMD5() throws IOException {
        SqlSession dbSession = DBSession.openSession();
        try {
            ImageMapper imageMapper = dbSession.getMapper(ImageMapper.class);
            return imageMapper.getImagesWithoutMD5();
        } finally {
            if (dbSession != null) {
                dbSession.close();
            }
        }
    }

    public void updateMD5(PictureEntity pictureEntity) throws IOException {
        SqlSession dbSession = DBSession.openSession();
        try {
            ImageMapper imageMapper = dbSession.getMapper(ImageMapper.class);
            imageMapper.updateMD5(pictureEntity);
            dbSession.commit();
        } finally {
            if (dbSession != null) {
                dbSession.close();
            }
        }
    }

    public void changePath(PictureEntity pictureEntity) throws IOException {
        SqlSession dbSession = DBSession.openSession();
        try {
            ImageMapper imageMapper = dbSession.getMapper(ImageMapper.class);
            imageMapper.changePath(pictureEntity);
            dbSession.commit();

        } finally {
            if (dbSession != null) {
                dbSession.close();
            }
        }
    }

    public List<ProductEntity> getProductsByImageId(int id) throws IOException {
        SqlSession dbSession = DBSession.openSession();
        try {
            ImageMapper imageMapper = dbSession.getMapper(ImageMapper.class);
            return imageMapper.getProductsByImageId(id);
        } finally {
            if (dbSession != null) {
                dbSession.close();
            }
        }
    }

    public List<BrandEntity> getBrandsByImage(int id) throws IOException {
        SqlSession dbSession = DBSession.openSession();
        try {
            ImageMapper imageMapper = dbSession.getMapper(ImageMapper.class);
            return imageMapper.getBrandsByImageId(id);
        } finally {
            if (dbSession != null) {
                dbSession.close();
            }
        }
    }

    public void setOriginalPictureForBrand(int imageId, int existentImageId, int brandId) throws IOException {
        SqlSession dbSession = DBSession.openSession();
        try {
            ImageMapper imageMapper = dbSession.getMapper(ImageMapper.class);
            imageMapper.setOriginalImageForBrandWithDuplicate(imageId, existentImageId, brandId);
            dbSession.commit();
        } finally {
            if (dbSession != null) {
                dbSession.close();
            }
        }
    }

    public void setOriginalPictureForProduct(int originalId, int existentImageId, int productId) throws IOException {
        SqlSession dbSession = DBSession.openSession();
        try {
            ImageMapper imageMapper = dbSession.getMapper(ImageMapper.class);

            //if (image == null) {
            imageMapper.setOriginalImageForProductWithDuplicate(originalId, existentImageId, productId);
            dbSession.commit();
            //}
        } finally {
            if (dbSession != null) {
                dbSession.close();
            }
        }
    }

    public void deletePicture(PictureEntity pictureEntity) throws IOException {
        SqlSession dbSession = DBSession.openSession();
        try {
            ImageMapper imageMapper = dbSession.getMapper(ImageMapper.class);
            imageMapper.deleteImage(pictureEntity.id);
            dbSession.commit();
        } finally {
            if (dbSession != null) {
                dbSession.close();
            }
        }
    }

    public int getImagesWithoutMD5TotalCount() throws IOException {
        SqlSession dbSession = DBSession.openSession();
        try {
            ImageMapper imageMapper = dbSession.getMapper(ImageMapper.class);
            return imageMapper.getImagesWithoutMD5TotalCount();
        } finally {
            if (dbSession != null) {
                dbSession.close();
            }
        }
    }

    public void clearMD5() throws IOException {
        SqlSession dbSession = DBSession.openSession();
        try {
            ImageMapper imageMapper = dbSession.getMapper(ImageMapper.class);
            imageMapper.clearMD5();
            dbSession.commit();

        } finally {
            if (dbSession != null) {
                dbSession.close();
            }
        }
    }

    public void addDuplicate(int originalImageId, int duplicateImageId) throws IOException {
        SqlSession session = DBSession.openSession();
        try {
            ImageMapper imageMapper = session.getMapper(ImageMapper.class);
            imageMapper.addDuplicate(originalImageId, duplicateImageId);
            session.commit();
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    public List<DuplicateEntity> getDuplicates() throws IOException {
        SqlSession session = DBSession.openSession();
        try {
            ImageMapper imageMapper = session.getMapper(ImageMapper.class);
            return imageMapper.getDuplicates();
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    public void readDuplicate(int id) throws IOException {
        SqlSession session = DBSession.openSession();
        try {
            ImageMapper imageMapper = session.getMapper(ImageMapper.class);
            imageMapper.readDuplicate(id);
            session.commit();
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    public PictureEntity getImage(int id) throws IOException {
        SqlSession session = DBSession.openSession();
        try {
            ImageMapper imageMapper = session.getMapper(ImageMapper.class);
            return imageMapper.getImage(id);
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    public PictureEntity getProductImage(int imageId, Integer productId) throws IOException {
        SqlSession session = DBSession.openSession();
        try {
            ImageMapper imageMapper = session.getMapper(ImageMapper.class);
            return imageMapper.getProductImage(productId, imageId);
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    public void deleteProductImage(int imageId, Integer productId) throws IOException {
        SqlSession session = DBSession.openSession();
        try {
            ImageMapper imageMapper = session.getMapper(ImageMapper.class);
            imageMapper.deleteProductImage(imageId, productId);
            session.commit();
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

}
